﻿<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <script>
        let ar2 = [`22￥`, ` 3￥`, `54￥`, `12￥`];
        ar2.sort(function (a, b) {
            return b.localeCompare(a);
        });
        console.log(ar2);

        let arr = [22, 3, 54, 12];
        arr.sort(); // 无参，默认按字符串排序
        console.log(arr);

        let arr1 = [22, 13, 4, 32];
        // 数字排序
        arr1.sort(function (a, b) {
            return a - b;
        });
        console.log(arr1);

        // 作业：对象的属性排序。
        // 商品的价格，距离，好评，综合
        let items = [
            { name: 'Edward', value: 21 },
            { name: 'Sharpe', value: 37 },
            { name: 'And', value: 45 },
            { name: 'The', value: -12 },
            { name: 'Magnetic', value: 13 },
            { name: 'Zeros', value: 37 }
        ];

        // sort by value
        //items.sort((a, b) => a.value - b.value);
        //console.log(items);

        items.sort((a, b) => {
            const nameA = a.name.toUpperCase(); // ignore upper and lowercase
            const nameB = b.name.toUpperCase(); // ignore upper and lowercase
            if (nameA < nameB) {
                return -1;
            }
            if (nameA > nameB) {
                return 1;
            }

            // names must be equal
            return 0;
        });
        console.log(items);
    </script>
</body>
</html>